/*
 *
 * Copyright (c) 2001-2018 泛微软件.
 * 泛微协同商务系统,版权所有.
 * 
 */


package com.engine.demo.web;


import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.demo.service.DemoService;
import com.engine.demo.service.impl.DemoServiceImpl;
import com.sun.media.Log;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;



public class DemoAction {

   
    //创建业务Service
    private DemoService getService(User user) {
        return ServiceUtil.getService(DemoServiceImpl.class, user);
    }
    private DemoService getService(){
        return ServiceUtil.getService(DemoServiceImpl.class);
    }

    //获取当前登录用户
    private User getUser(HttpServletRequest request, HttpServletResponse response)
    {
    	return HrmUserVarify.getUser(request, response);
    }

    /**
     * @description ：查询
     * @param       ：request, response
     * @return      ：String
     */
    @GET
    @Path("/queryList")
    @Produces(MediaType.TEXT_PLAIN)
    public String queryEMList(@Context HttpServletRequest request, @Context HttpServletResponse response){

        WeaResultMsg result= new WeaResultMsg(false);
        try{
            //实例化Service并调用业务类处理
        	result.assignMap(getService().queryList(ParamUtil.request2Map(request),getUser(request, response)));
            //result.success();
        }catch(Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage());//code错误码可以自己定义
        }
        return result.toString();
    }

    /**
     * @description ：查询一条信息
     * @param       ：request, response
     * @return      ：String
     */
    @GET
    @Path("/queryById")
    @Produces(MediaType.TEXT_PLAIN)
    public String queryById(@Context HttpServletRequest request, @Context HttpServletResponse response){
    	WeaResultMsg result= new WeaResultMsg(false);
        try{
            //实例化Service并调用业务类处理
        	result.assignMap(getService().queryById(ParamUtil.request2Map(request),getUser(request, response)));
            //result.success();
        }catch(Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage());//code错误码可以自己定义
        }
        return result.toString();
    }
    
    
    /**
     * @description ：查询一条Form信息
     * @param       ：request, response
     * @return      ：String
     */
    @GET
    @Path("/getFormById")
    @Produces(MediaType.TEXT_PLAIN)
    public String getFormById(@Context HttpServletRequest request, @Context HttpServletResponse response){
    	WeaResultMsg result= new WeaResultMsg(false);
        try{
            //实例化Service并调用业务类处理
        	result.assignMap(getService().getFormById(ParamUtil.request2Map(request),getUser(request, response)));
            //result.success();
        }catch(Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage());//code错误码可以自己定义
        }
        return result.toString();
    }
    
    /**
     * @description ：更新
     * @param       ：request, response
     * @return      ：String
     */
    @POST
    @Path("/update")
    @Produces(MediaType.TEXT_PLAIN)
    public String update(@Context HttpServletRequest request,@Context HttpServletResponse response){
    	WeaResultMsg result= new WeaResultMsg(false);
        try{

        	result.assignMap(getService().update(ParamUtil.request2Map(request),getUser(request, response)));
 
        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage());//code错误码可以自己定义
        }
        return result.toString();
    }
    
    /**
     * @description ：删除
     * @param       ：request, response
     * @return      ：String
     */
    @GET
    @Path("/delete")
    @Produces(MediaType.TEXT_PLAIN)
    public String delete(@Context HttpServletRequest request,@Context HttpServletResponse response){

    	WeaResultMsg result= new WeaResultMsg(false);
        try{

            result.assignMap(getService().delete(ParamUtil.request2Map(request),getUser(request, response)));

        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage());//code错误码可以自己定义
        }
        return result.toString();
    }

    /**
     * @description ：添加
     * @param       ：request, response
     * @return      ：String
     */
    @POST
    @Path("/add")
    @Produces(MediaType.TEXT_PLAIN)
    public String add(@Context HttpServletRequest request,@Context HttpServletResponse response){
    	WeaResultMsg result= new WeaResultMsg(false);
        try{

            result.assignMap(getService().add(ParamUtil.request2Map(request),getUser(request, response)));

        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage(), -1);//code错误码可以自己定义
        }
        return result.toString();
    }

    /**
     * @description ：更改状态
     * @param       ：[request, response]
     * @return      ：java.lang.String
     */
    @GET
    @Path("/changeStatus")
    @Produces(MediaType.TEXT_PLAIN)
    public String changeStatus(@Context HttpServletRequest request,@Context HttpServletResponse response){
    	WeaResultMsg result= new WeaResultMsg(false);
        try{
            result.assignMap(getService().changeStatus(ParamUtil.request2Map(request),getUser(request, response)));

        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage(), -1);//code错误码可以自己定义
        }
        return result.toString();
    }

    /**
     * @description ：高级搜索
     * @param       ：request, response
     * @return      ：String
     */
    @GET
    @Path("/getCondition")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCondition(@Context HttpServletRequest request,@Context HttpServletResponse response){

    	WeaResultMsg result= new WeaResultMsg(false);
        try{

            result.assignMap(getService().getCondition(ParamUtil.request2Map(request),getUser(request, response)));

        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage(), -1);//code错误码可以自己定义
        }
        return result.toString();
    }
    
    
    @GET
    @Path("/getTest")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTest(@Context HttpServletRequest request,@Context HttpServletResponse response){

    	WeaResultMsg result= new WeaResultMsg(false);
        try{

            result.assignMap(getService().getTest(ParamUtil.request2Map(request),getUser(request, response)));

        }catch (Exception e){
            e.printStackTrace();
            Log.error(e);
            result.fail(e.getLocalizedMessage(), -1);//code错误码可以自己定义
        }
        return result.toString();
    }
    
}
